<?php

namespace Admin\Models\user;

use Admin\Models\BaseModel;
use Admin\Models\school\XhschoolInfo;

class XhsysUserHistory extends BaseModel
{
    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSys_UserHistory';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolGroup[]|XhschoolGroup
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolGroup
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 获取用户历史
     *
     * @param $userId
     * @return array
     *
     * create by sqb 20190107
     */
    public static function getHistoryByUserId($userId)
    {
        $history = self::find([
            'conditions' => 'iUserId = :userId:',
            'bind' => ['userId' => $userId]
        ]);

        if ($history) {
            $history = $history->toArray();
            foreach ($history as &$item) {
                $item['sSchoolName'] = XhschoolInfo::getSchoolNameById($item['iSchoolId']);
            }

            return $history;
        }

        return [];
    }
}
